package com.be.beadmin.quality.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.quality.dto.input.ProductSelfCheckInforDto;
import com.be.beadmin.quality.dto.output.ProductSelfCheckInforEx;
import com.be.beadmin.quality.service.ProductSelfCheckInforService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author zjm
* @date 2023-03-19
**/
@Api(tags = {"生产执行-自检上传"})
@RestController
public class ProductSelfCheckInforController {

    @Autowired
    private  ProductSelfCheckInforService productSelfCheckInforService;


    @PostMapping(value = "/product/getProductSelfCheckInforByPage")
    public ResultModel getProductSelfCheckInforByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(productSelfCheckInforService.getProductSelfCheckInforByPage(pageEntity)) ;
    }
    @ApiParam(value = "根据工序id获取改工序的自检信息")
    @PostMapping(value = "/product/getProductSelfCheckInforByProcessId")
    public ResultModel getProductSelfCheckInforByProcessId(@RequestParam String processId)  {
        return ResultModel.success(productSelfCheckInforService.getProductSelfCheckInforByProcessId(processId)) ;
    }
    @PostMapping("/product/addProductSelfCheckInfor")
    public ResultModel addProductSelfCheckInfor(@RequestBody ProductSelfCheckInforDto productSelfCheckInforDto){
        productSelfCheckInforService.addProductSelfCheckInfor(productSelfCheckInforDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/product/delProductSelfCheckInfor")
    public ResultModel delProductSelfCheckInfor(@RequestBody ProductSelfCheckInforDto productSelfCheckInforDto){
        productSelfCheckInforService.delProductSelfCheckInfor(productSelfCheckInforDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/product/updateProductSelfCheckInfor")
    public ResultModel updateProductSelfCheckInfor(@RequestBody ProductSelfCheckInforDto productSelfCheckInforDto){
        productSelfCheckInforService.updateProductSelfCheckInfor(productSelfCheckInforDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/product/exportProductSelfCheckInfor")
    public void exportProductSelfCheckInfor(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<ProductSelfCheckInforEx> productSelfCheckInforPage = productSelfCheckInforService.getProductSelfCheckInforByPage(pageEntity);
        List<ProductSelfCheckInforEx> productSelfCheckInforExes = productSelfCheckInforPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("ProductSelfCheckInfor接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ProductSelfCheckInforEx.class).sheet("sheet1").doWrite(productSelfCheckInforExes);
    }
}